package com.upf1sh.config;

import com.upf1sh.config.SensorDataJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

@Configuration
public class QuartzConfig {

    @Bean
    public SpringBeanJobFactory springBeanJobFactory() {
        return new SpringBeanJobFactory();
    }

    @Bean
    public SchedulerFactoryBean schedulerFactory(Trigger sensorDataJobTrigger, JobDetail sensorDataJobDetail) {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setJobFactory(springBeanJobFactory());
        factory.setJobDetails(sensorDataJobDetail);
        factory.setTriggers(sensorDataJobTrigger);
        return factory;
    }

    @Bean
    public JobDetailFactoryBean sensorDataJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(SensorDataJob.class);
        factoryBean.setDurability(true);
        return factoryBean;
    }

    @Bean
    public CronTriggerFactoryBean sensorDataJobTrigger(JobDetail sensorDataJobDetail) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(sensorDataJobDetail);
        // 每天 22:40 执行
        factoryBean.setCronExpression("0 16 23 * * ?");
        return factoryBean;
    }
}